package com.duolaibei.business.view.goods;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.duolaibei.business.R;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.activity.MyCaptureActivity;
import com.duolaibei.business.view.goods.mode.FormatsBean;
import com.lwkandroid.imagepicker.ImagePicker;
import com.lwkandroid.imagepicker.data.ImageBean;
import com.lwkandroid.imagepicker.data.ImagePickType;
import com.lwkandroid.imagepicker.utils.GlideImagePickerDisplayer;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.List;
import java.util.Objects;

public class GoodsAddActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @Inject(R.id.back)
    private RelativeLayout mBack;
    @Inject(R.id.title)
    private TextView mTxtTitle;
    @Inject(R.id.btn_goods_save)
    private TextView mBtnSave;
    @Inject(R.id.img_goods_shangchuan)
    private ImageView mImgShangchuan;
    @Inject(R.id.scan_goods_code)
    private ImageView mImgScanCode;
    @Inject(R.id.goods_code)
    private EditText mEditGoodsCode;
    @Inject(R.id.goods_type)
    private RelativeLayout mRLGoodsType;
    @Inject(R.id.type_content)
    private TextView mTxtContent;
    @Inject(R.id.rl_goods_unit)
    private RelativeLayout mRlGoodsUnit;
    @Inject(R.id.goods_unit)
    private TextView mtxtGoodsUnit;
    @Inject(R.id.rl_format)
    private RelativeLayout mRlFormat;//规格设置
    @Inject(R.id.rl_nums)
    private RelativeLayout mRlNums;//批量设置
    @Inject(R.id.rl_goods_price)
    private RelativeLayout mRlprice;//商品价格
    @Inject(R.id.switch_setup)
    private Switch mSwtichSetup;

    private final int REQUEST_PHOTO_CODE = 111;
    private final int REQUEST_GOODS_CODE = 222;
    private final int REQUEST_GOODS_TYPE = 333;
    private final int REQUEST_GOODS_UNIT = 444;
    private final int REQUEST_GOODS_FORMAT = 555;
    private final int REQUEST_GOODS_BATCH = 666;
    private String cachePath;

    private String resultImg;//商品图片地址
    private String goodsCode;//商品条码

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_goods_add);
    }

    @Override
    protected void initViews() {
        mTxtTitle.setText("新增商品");
        cachePath = getExternalFilesDir(null) + "/mypics/photos/";
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
    }

    @Override
    protected void onSetListener() {
        mBack.setOnClickListener(getThis());
        mBtnSave.setOnClickListener(getThis());
        mImgShangchuan.setOnClickListener(getThis());
        mImgScanCode.setOnClickListener(getThis());
        mRLGoodsType.setOnClickListener(getThis());
        mRlGoodsUnit.setOnClickListener(getThis());
        mRlFormat.setOnClickListener(getThis());
        mRlNums.setOnClickListener(getThis());
        mSwtichSetup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() == R.id.switch_setup) {
            if (isChecked) {
                mRlFormat.setVisibility(View.VISIBLE);
                mRlprice.setVisibility(View.GONE);
            } else {
                mRlFormat.setVisibility(View.GONE);
                mRlNums.setVisibility(View.GONE);
                mRlprice.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        } else if (v.getId() == R.id.btn_goods_save) {

        } else if (v.getId() == R.id.img_goods_shangchuan) {
            //发起图片选择
            new ImagePicker()
                    .pickType(ImagePickType.SINGLE) //设置选取类型(拍照ONLY_CAMERA、单选SINGLE、多选MUTIL)
//                    .maxNum(9) //设置最大选择数量(此选项只对多选生效，拍照和单选都是1，修改后也无效)
                    .needCamera(true) //是否需要在界面中显示相机入口(类似微信那样)
                    .cachePath(cachePath) //自定义缓存路径(拍照和裁剪都需要用到缓存)
                    .doCrop(1, 1, 300, 300) //裁剪功能需要调用这个方法，多选模式下无效，参数：aspectX,aspectY,outputX,outputY
                    .displayer(new GlideImagePickerDisplayer()) //自定义图片加载器，默认是Glide实现的,可自定义图片加载器
                    .start(this, REQUEST_PHOTO_CODE); //自定义RequestCode
        } else if (v.getId() == R.id.scan_goods_code) {
            Intent intent = new Intent(getThis(), MyCaptureActivity.class);
            intent.putExtra("title", "扫一扫");
            startActivityForResult(intent, REQUEST_GOODS_CODE);
        } else if (v.getId() == R.id.goods_type) {
            Intent intent = new Intent(getThis(), TypesManegeActiviy.class);
            intent.putExtra("class_sign", "GoodsManageActivity");
            startActivityForResult(intent, REQUEST_GOODS_TYPE);
        } else if (v.getId() == R.id.rl_goods_unit) {
            Intent intent = new Intent(getThis(), UnitsManegeActivity.class);
            startActivityForResult(intent, REQUEST_GOODS_UNIT);
        } else if (v.getId() == R.id.rl_format) {
            Intent intent = new Intent(getThis(), FormatsActiviy.class);
            startActivityForResult(intent, REQUEST_GOODS_FORMAT);
        } else if (v.getId() == R.id.rl_nums) {
            Intent intent = new Intent(getThis(), BatchSetupActivity.class);
            startActivityForResult(intent, REQUEST_GOODS_BATCH);
        }
    }

    //保存商品
    private void saveGood() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_PHOTO_CODE && resultCode == RESULT_OK && data != null) {
            //多选
            List<ImageBean> resultList = Objects.requireNonNull(data.getExtras()).getParcelableArrayList(ImagePicker.INTENT_RESULT_DATA);
            if (resultList != null) {
                resultImg = resultList.get(0).getImagePath();
                Glide.with(getThis()).asDrawable().load(resultImg).into(mImgShangchuan);
            }
        }
        if (requestCode == REQUEST_GOODS_CODE && resultCode == RESULT_OK && data != null) {
            //处理扫描结果（在界面上显示）
            Bundle bundle = data.getExtras();
            if (bundle == null) {
                return;
            }
            if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                goodsCode = bundle.getString(CodeUtils.RESULT_STRING);
                mEditGoodsCode.setText(goodsCode);
            } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                Toast.makeText(getThis(), "解析二维码失败", Toast.LENGTH_LONG).show();
            }
        }
        if (requestCode == REQUEST_GOODS_TYPE && resultCode == RESULT_OK && data != null) {
            String goodsType = data.getStringExtra("goods_type");
            if (!CommonUtil.isEmpty(goodsType)) {
                mTxtContent.setText(goodsType);
            }
        }
        if (requestCode == REQUEST_GOODS_UNIT && resultCode == RESULT_OK && data != null) {
            String goodsType = data.getStringExtra("unit_goods");
            if (!CommonUtil.isEmpty(goodsType)) {
                mtxtGoodsUnit.setText(goodsType);
            }
        }
        if (requestCode == REQUEST_GOODS_FORMAT && resultCode == RESULT_OK && data != null) {
            //接受该商品设置的规格
            List<FormatsBean> list = (List<FormatsBean>) data.getSerializableExtra("format_checked");
            if (list.size() != 0) {
                //规格不为空时，显示批量设置价格和商品条码
                mRlNums.setVisibility(View.VISIBLE);
            } else {
                mRlNums.setVisibility(View.GONE);
            }
        }
        if (requestCode == REQUEST_GOODS_BATCH && resultCode == RESULT_OK && data != null) {

        }
    }
}
